<html>
<head><meta charset="utf-8"><title>volatile and libm · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html">volatile and libm</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169712406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169712406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169712406">(Jul 05 2019 at 14:22)</a>:</h4>
<p>So libm uses <code>volatile</code> all over the place to force side-effects, e.g., </p>
<div class="codehilite"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="n">condition</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="c1">// set overflow flags:</span>
<span class="w">     </span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w">  </span><span class="p">).</span><span class="n">read_volatile</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169712444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169712444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169712444">(Jul 05 2019 at 14:23)</a>:</h4>
<p>If x * x overflows, some flags in a register might be set. libm wants to set those flags, so it does things like this</p>



<a name="169712550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169712550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169712550">(Jul 05 2019 at 14:24)</a>:</h4>
<p>It has a macro called <code>force_eval!</code> for it, which is defined as:</p>
<div class="codehilite"><pre><span></span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">force_eval</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$e</span>:<span class="nc">expr</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span>::<span class="n">core</span>::<span class="n">ptr</span>::<span class="n">read_volatile</span><span class="p">(</span><span class="o">&amp;</span><span class="cp">$e</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169712804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169712804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169712804">(Jul 05 2019 at 14:28)</a>:</h4>
<p>Spot the problem: </p>
<div class="codehilite"><pre><span></span><span class="c1">// mod foo:</span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="c1">// mod bar:</span>
<span class="n">force_eval</span><span class="o">!</span><span class="p">(</span><span class="n">foo</span><span class="p">());</span><span class="w">  </span><span class="c1">// look ma, no unsafe { }</span>
</pre></div>



<a name="169712979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169712979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169712979">(Jul 05 2019 at 14:30)</a>:</h4>
<p>AFAICT the only way to do a volatile load on a reference, is to go via a pointer first, so there is no way to do that without unsafe code =/</p>



<a name="169726692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169726692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169726692">(Jul 05 2019 at 18:13)</a>:</h4>
<p>Oh. Now I know what I'm doing in Underhanded Rust competition if they ever hold another one.</p>



<a name="169726765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169726765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169726765">(Jul 05 2019 at 18:14)</a>:</h4>
<p>I'm putting something unsafe under <code>lazy_static!</code></p>



<a name="169740346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169740346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169740346">(Jul 05 2019 at 22:58)</a>:</h4>
<p>if lazy static allows you to call unsafe code without using unsafe, then that sounds like a cool thing</p>



<a name="169757447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169757447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169757447">(Jul 06 2019 at 08:03)</a>:</h4>
<p>"cool"</p>



<a name="169798461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169798461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169798461">(Jul 07 2019 at 06:27)</a>:</h4>
<p>wait, what is wrong with that?</p>



<a name="169798503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169798503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169798503">(Jul 07 2019 at 06:28)</a>:</h4>
<p>that gets turned into <code>read_volatile(&amp;(foo())) -&gt; read_volatile(&amp;0) -&gt; 0</code></p>



<a name="169822744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169822744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169822744">(Jul 07 2019 at 19:12)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> you could call the macro with <code>{ unsafe_fn(); expr }</code> and you wouldn't need an unsafe block for calling <code>unsafe_fn</code></p>



<a name="169822802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169822802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169822802">(Jul 07 2019 at 19:14)</a>:</h4>
<p>beyond that, there is nothing wrong with that; the point I tried to make is that this shouldn't be unsafe, the reason why this is unsafe is because we don't have <code>volatile_read</code> methods implemented for <code>&amp;T</code>, so one has to cast to a raw pointer, and then dereference the raw pointer</p>



<a name="169828653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/169828653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#169828653">(Jul 07 2019 at 22:15)</a>:</h4>
<p>oh, got it; yeah, makes sense now</p>



<a name="170643280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/170643280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#170643280">(Jul 11 2019 at 15:20)</a>:</h4>
<blockquote>
<p>beyond that, there is nothing wrong with that; the point I tried to make is that this shouldn't be unsafe, the reason why this is unsafe is because we don't have <code>volatile_read</code> methods implemented for <code>&amp;T</code>, so one has to cast to a raw pointer, and then dereference the raw pointer</p>
</blockquote>
<p>OTOH it is very easy to write a safer wrapper in a library</p>



<a name="170643283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/170643283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#170643283">(Jul 11 2019 at 15:20)</a>:</h4>
<p>this does not <em>have to</em> be a libcore thing</p>



<a name="170662771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%20and%20libm/near/170662771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.20and.20libm.html#170662771">(Jul 11 2019 at 19:09)</a>:</h4>
<p>simple safe-wrapping operations for core types that can be in core should be in core. As a person who has spent all week working on a core only safe wrapping crate, I would be happy if core went around finding other useful core-compatible safe-wrapping stuff and just absorbing it so that everyone can benefit instead of trying to convince people to use a crate they've never heard of</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>